package com.css.dzj.dzjwgr.common.menu;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.css.dzj.dzjwgr.common.util.R;
import com.css.dzj.dzjwgr.common.util.Restful.RestfulUtil;
import com.css.dzj.dzjwgr.common.util.ServiceUtil;
import com.css.dzj.dzjwgr.sys.user.controller.LoginController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.HttpMethod;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/menu")
@PropertySource(value = "classpath:base.properties")
public class GetMenuController {
    @Autowired
    private HttpServletRequest request;
    @Value("${BASE_URL}")
    private String BASE_URL;
    @Autowired
    LoginController loginController;
    @GetMapping("/getMenu")
    public R getMenu(HttpServletResponse response) {
        HttpSession session = request.getSession();
        String id = session.getAttribute("userId").toString();
        String appCode = null;
        List<Map<String, Object>> sysApp = ServiceUtil.getSysApp(request);
        for (Map<String, Object> app : sysApp) {
            if (app.get("appName").equals("外国组织或者个人在华从事地震监测活动监管")) {
                appCode = app.get("appCode").toString();
            }
        }
        try {
            // 根据用户id和子系统code获取菜单信息
            String url = BASE_URL + "/platform/api/sys/menus/userid/" + id + "/appcode/" + appCode;
            Map<String, String> map = new HashMap<>();
        /*map.put("userId", id);
        map.put("appId", appId);*/
            HttpMethod method = HttpMethod.GET;
            Map body = RestfulUtil.sendRestful(request, map, url, method);

            String data = JSONObject.toJSONString(body.get("data"));

            JSONArray result = JSONArray.parseArray(data);

            JSONArray fastBtn = new JSONArray();
            addFastEntry(result, fastBtn);

            return R.ok().put("data", data).put("fastBtn", fastBtn);
        }catch (RuntimeException e){
            // session中token失效，退出登录
            loginController.loginOut(response);
            return R.error(e.getMessage());
        }
    }

    /**
     * base系统接口
     *
     * @param mi
     * @param fb
     */
    public void addFastEntry(JSONArray mi, JSONArray fb) {
        for (Object obj : mi) {
            JSONObject one = (JSONObject) obj;
            if (one.get("subMenu")!= null&&((JSONArray)one.get("subMenu")).size()>0) {
                JSONArray jsa = (JSONArray) one.get("subMenu");
                addFastEntry(jsa, fb);
            } else {
                JSONObject node = new JSONObject();
                node.put("menuId", one.get("menuId"));
                node.put("menuName", one.get("menuName"));
                node.put("menuUrl", one.get("menuUrl"));
                fb.add(node);
            }
        }
    }
}
